HAL JPEG Typical use cases for migration

This section provides information about HAL JPEG use cases for STM32 HAL1 to HAL2 migration.

The JPEG encoding and decoding process is almost the same between HAL1 and HAL2, with only minor differences :

  • In HAL1, enabling the JPEG core and setting the Huffman table registers is done by the HAL_JPEG_Init function, while in HAL2 these are handled by the HAL_JPEG_SetConfigEncoding function.

  • Some APIs are renamed to align with HAL2 coding and naming rules.

  • In HAL2, pause and resume APIs are split into dedicated functions for input or output buffers.

  • In HAL1, DMA channels are linked to the JPEG handle using the __HAL_LINKDMA macro. In HAL2, the linkage is handled by the HAL_JPEG_SetTxDMA and HAL_JPEG_SetRxDMA functions.

Encoding use cases are almost the same as decoding. The main difference is to replace decoding functions ( HAL_JPEG_Decode, HAL_JPEG_Decode_IT, HAL_JPEG_Decode_DMA) with encoding functions ( HAL_JPEG_Encode, HAL_JPEG_Encode_IT, HAL_JPEG_Encode_DMA).

HAL JPEG decoding in polling mode

Sequence diagram

Process Decoding polling mode

@startuml
' To add a number by line
'autonumber

' Fix order of each column
participant "User Application" as user
participant "HAL JPEG" as JPEG

user->JPEG : <b>HAL_JPEG_Init()</b>
user <-- JPEG : HAL_OK
user->JPEG : <b>HAL_JPEG_Decode()</b>
user <-- JPEG : HAL_OK

user <- JPEG : <b>HAL_JPEG_InfoReadyCallback()</b>\nIs asserted when the JPEG peripheral successfully parse the JPEG header

user <- JPEG : <b>HAL_JPEG_GetDataCallback()</b>\nIs asserted when the input buffer has been consumed by the peripheral and to ask for a new data chunk
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, when HAL_JPEG_GetDataCallback is received, the user can pause the process and change the input buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigInputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, the user can resume the process after changing the input buffer.
  user <-- JPEG :
end

user <- JPEG : <b>HAL_JPEG_DataReadyCallback()</b>\nIs asserted when the HAL JPEG driver has filled the given output buffer with the given size
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, when HAL_JPEG_DataReadyCallback is received, the user can pause the process and change the output buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigOutputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, the user can resume the process after changing the output buffer.
  user <-- JPEG :
end

user <- JPEG : <b>HAL_JPEG_DecodeCpltCallback()</b>\nIs asserted when the HAL JPEG driver has ended the current JPEG decoding operation


@enduml

code snippet

Example of code in HAL 1

Example of code in HAL 2

/* Declaration of the JPEG handle */
JPEG_HandleTypeDef hjpeg;
/* Declaration of the HAL status */
HAL_StatusTypeDef jpeg_info_status;



/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // JPEG image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Decoded image output buffer

/* Ensure that all required peripherals (JPEG) are configured properly before starting decoding */
/* This includes clock enabling */

/* Initialize the JPEG peripheral */
HAL_JPEG_Init(&hjpeg);

/* Start JPEG Decoding in polling mode */
HAL_JPEG_Decode(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer), TIMEOUT);

/* Callback invoked when the JPEG peripheral successfully parse the JPEG header */
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
{
    /* the JPEG decoding such as image width, height, color space, and subsampling
    are available in the pInfo structure and it can be used by the user */
}




/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_ConfigInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut,
                                uint32_t OutDataLength)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_ConfigOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
/* Declaration of the JPEG handle */
hal_jpeg_handle_t hjpeg;
/* Declaration of the JPEG config */
hal_jpeg_config_t jpeg_config;
/* Declaration of the HAL status */
HAL_StatusTypeDef jpeg_info_status;

/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // JPEG image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Decoded image output buffer

/* Ensure that all required peripherals (JPEG) are configured properly before starting encoding */
/* This includes clock enabling */

/* Initialize the JPEG peripheral and associate the handle */
HAL_JPEG_Init(&hjpeg, HAL_JPEG);

/* Start JPEG Decoding in polling mode */
HAL_JPEG_Decode(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer), TIMEOUT);

/* Callback invoked when the JPEG peripheral successfully parse the JPEG header */
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* The HAL_JPEG_InfoReadyCallback informs the user that the JPEG information
    are ready and can be retrieved by calling the HAL_JPEG_GetLastDecodeInfo function */
    /* Optional: use HAL_JPEG_GetLastDecodeInfo function to get image parameters */
    jpeg_info_status = HAL_JPEG_GetLastDecodeInfo(hjpeg, &jpeg_config);
    /* User can now use jpeg_config to get image parameters */
}

/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_PauseInputBuffer(hjpeg);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_UpdateInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_ResumeInputBuffer(hjpeg);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(hal_jpeg_handle_t *hjpeg, const uint8_t *p_data_out,
                                uint32_t out_data_length_byte)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_PauseOutputBuffer(hjpeg);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_UpdateOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_ResumeOutputBuffer(hjpeg);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

HAL JPEG decoding in interrupt mode

Sequence diagram

Process Decoding interrupt mode

@startuml
' To add a number by line
'autonumber

' Fix order of each column
participant "User Application" as user
participant "HAL JPEG" as JPEG
participant "HAL DMA" as DMA
participant "NVIC" as nvc
participant "HW" as hw

user->JPEG : <b>HAL_JPEG_Init()</b>
user <-- JPEG : HAL_OK
user->JPEG : <b>HAL_JPEG_Decode_IT()</b>
user <-- JPEG : HAL_OK

hw -> nvc : <<Header parsing done interrupt>>
nvc -> JPEG : HAL_JPEG_IRQHandler()

user <- JPEG : <b>HAL_JPEG_InfoReadyCallback()</b>\nIs asserted when the JPEG peripheral successfully parse the JPEG header

hw -> nvc : <<Input FIFO threshold interrupt>>
nvc -> JPEG : HAL_JPEG_IRQHandler()

user <- JPEG : <b>HAL_JPEG_GetDataCallback()</b>\nIs asserted when the input buffer has been consumed by the peripheral and to ask for a new data chunk
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, when HAL_JPEG_GetDataCallback is received, the user can pause the process and change the input buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigInputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, the user can resume the process after changing the input buffer.
  user <-- JPEG :
end

hw -> nvc : <<Output FIFO threshold interrupt>>
nvc -> JPEG : HAL_JPEG_IRQHandler()

user <- JPEG : <b>HAL_JPEG_DataReadyCallback()</b>\nIs asserted when the HAL JPEG driver has filled the given output buffer with the given size
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, when HAL_JPEG_DataReadyCallback is received, the user can pause the process and change the output buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigOutputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, the user can resume the process after changing the output buffer.
  user <-- JPEG :
end

hw -> nvc : <<End of conversion interrupt>>
nvc -> JPEG : HAL_JPEG_IRQHandler()

user <- JPEG : <b>HAL_JPEG_DecodeCpltCallback()</b>\nIs asserted when the HAL JPEG driver has ended the current JPEG decoding operation


@enduml

code snippet

Example of code in HAL 1

Example of code in HAL 2

/* Declaration of the JPEG handle */
JPEG_HandleTypeDef hjpeg;
/* Declaration of the HAL status */
HAL_StatusTypeDef jpeg_info_status;



/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // JPEG image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Decoded image output buffer

/* Ensure that all required peripherals are configured properly before starting decoding */
/* This includes clock enabling, interrupt priority setup */

/* Initialize the JPEG peripheral */
HAL_JPEG_Init(&hjpeg);

/* Start JPEG Decoding in interrupt mode */
HAL_JPEG_Decode_IT(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer));

/* Callback invoked when the JPEG peripheral successfully parse the JPEG header */
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
{
    /* the JPEG decoding  such as image width, height, color space, and subsampling
    are available in the pInfo structure and it can be used by the user */
}




/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_ConfigInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut,
                                uint32_t OutDataLength)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_ConfigOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
/* Declaration of the JPEG handle */
hal_jpeg_handle_t hjpeg;
/* Declaration of the JPEG config */
hal_jpeg_config_t jpeg_config;
/* Declaration of the HAL status */
HAL_StatusTypeDef jpeg_info_status;

/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // JPEG image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Decoded image output buffer

/* Ensure that all required peripherals (JPEG, IT) are configured properly before starting encoding */
/* This includes clock enabling, interrupt priority setup */

/* Initialize the JPEG peripheral and associate the handle */
HAL_JPEG_Init(&hjpeg, HAL_JPEG);

/* Start JPEG Decoding in interrupt mode */
HAL_JPEG_Decode_IT(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer));

/* Callback invoked when the JPEG peripheral successfully parse the JPEG header */
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* The HAL_JPEG_InfoReadyCallback informs the user that the JPEG information
    are ready and can be retrieved by calling the HAL_JPEG_GetLastDecodeInfo function */
    /* Optional: use HAL_JPEG_GetLastDecodeInfo function to get image parameters */
    jpeg_info_status = HAL_JPEG_GetLastDecodeInfo(hjpeg, &jpeg_config);
    /* User can now use jpeg_config to get image parameters */
}

/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_PauseInputBuffer(hjpeg);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_UpdateInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_ResumeInputBuffer(hjpeg);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(hal_jpeg_handle_t *hjpeg, const uint8_t *p_data_out,
                                uint32_t out_data_length_byte)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_PauseOutputBuffer(hjpeg);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_UpdateOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_ResumeOutputBuffer(hjpeg);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

HAL JPEG decoding in DMA mode

Sequence diagram

Process Decoding DMA mode

@startuml
' To add a number by line
'autonumber

' Fix order of each column
participant "User Application" as user
participant "HAL JPEG" as JPEG
participant "HAL DMA" as DMA
participant "NVIC" as nvc
participant "HW" as hw

user->JPEG : <b>HAL_JPEG_Init()</b>
user <-- JPEG : HAL_OK
JPEG->DMA : <b>__HAL_LINKDMA()</b>
user->JPEG : <b>HAL_JPEG_Decode_DMA()</b>
user <-- JPEG : HAL_OK

hw -> nvc : <<Header parsing done interrupt>>
nvc -> JPEG : HAL_DMA_IRQHandler()

user <- JPEG : <b>HAL_JPEG_InfoReadyCallback()</b>\nIs asserted when the JPEG peripheral successfully parse the JPEG header
group #LightYellow Optional
  user -> JPEG : HAL_JPEG_GetInfo()
  user <-- JPEG :
end

hw -> nvc : <<DMAIn Transfer complete interrupt>>
nvc -> DMA : HAL_DMA_IRQHandler()
DMA -> JPEG : JPEG_DMAInCpltCallback()

user <- JPEG : <b>HAL_JPEG_GetDataCallback()</b>\nIs asserted when the input buffer has been consumed by the peripheral and to ask for a new data chunk
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, when HAL_JPEG_GetDataCallback is received, the user can pause the process and change the input buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigInputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, the user can resume the process after changing the input buffer.
  user <-- JPEG :
end

hw -> nvc : <<DMAOut Transfer complete interrupt>>
nvc -> DMA : HAL_DMA_IRQHandler()
DMA -> JPEG : JPEG_DMAOutCpltCallback()

user <- JPEG : <b>HAL_JPEG_DataReadyCallback()</b>\nIs asserted when the HAL JPEG driver has filled the given output buffer with the given size
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, when HAL_JPEG_DataReadyCallback is received, the user can pause the process and change the output buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigOutputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, the user can resume the process after changing the output buffer.
  user <-- JPEG :
end

hw -> nvc : <<End of conversion interrupt>>
nvc -> JPEG : HAL_JPEG_IRQHandler()

user <- JPEG : <b>HAL_JPEG_DecodeCpltCallback()</b>\nIs asserted when the HAL JPEG driver has ended the current JPEG decoding operation


@enduml

code snippet

Example of code in HAL 1

Example of code in HAL 2

/* Declaration of the JPEG handle */
JPEG_HandleTypeDef hjpeg;
/* Declaration of the HAL status */
HAL_StatusTypeDef jpeg_info_status;



/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // JPEG image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Decoded image output buffer

/* Ensure that all required peripherals (JPEG, DMA) are configured properly before starting decoding */
/* This includes clock enabling, interrupt priority setup. */

/* Initialize the JPEG peripheral */
HAL_JPEG_Init(&hjpeg);

/* Start JPEG Decoding in DMA mode */
HAL_JPEG_Decode_DMA(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer));

 /* Callback invoked when the JPEG peripheral successfully parse the JPEG header */
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
{
    /* the JPEG decoding  such as image width, height, color space, and subsampling
    are available in the pInfo structure and it can be used by the user */
}




/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_ConfigInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut,
                                uint32_t OutDataLength)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_ConfigOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
/* Declaration of the JPEG handle */
hal_jpeg_handle_t hjpeg;
/* Declaration of the JPEG config */
hal_jpeg_config_t jpeg_config;
/* Declaration of the HAL status */
HAL_StatusTypeDef jpeg_info_status;

/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // JPEG image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Decoded image output buffer

/* Ensure that all required peripherals (JPEG, DMA) are configured properly before starting encoding */
/* This includes clock enabling, interrupt priority setup. */

/* Initialize the JPEG peripheral and associate the handle */
HAL_JPEG_Init(&hjpeg, HAL_JPEG);

/* Start JPEG Decoding in DMA mode */
HAL_JPEG_Decode_DMA(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer));

/* Callback invoked when the JPEG peripheral successfully parse the JPEG header */
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* The HAL_JPEG_InfoReadyCallback informs the user that the JPEG information
    are ready and can be retrieved by calling the HAL_JPEG_GetLastDecodeInfo function */
    /* Optional: use HAL_JPEG_GetLastDecodeInfo function to get image parameters */
    jpeg_info_status = HAL_JPEG_GetLastDecodeInfo(hjpeg, &jpeg_config);
    /* User can now use jpeg_config to get image parameters */
}

/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_PauseInputBuffer(hjpeg);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_UpdateInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_ResumeInputBuffer(hjpeg);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(hal_jpeg_handle_t *hjpeg, const uint8_t *p_data_out,
                                uint32_t out_data_length_byte)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_PauseOutputBuffer(hjpeg);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_UpdateOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_ResumeOutputBuffer(hjpeg);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

Related examples in HAL2

Example Name

Description

hal_jpeg_decoding_dma

How to decode a JPEG image from internal FLASH using the JPEG hardware decoder in DMA mode and save it to internal FLASH.


HAL JPEG encoding in DMA mode

Sequence diagram

Process Encoding DMA mode

@startuml
' To add a number by line
'autonumber

' Fix order of each column
participant "User Application" as user
participant "HAL JPEG" as JPEG
participant "HAL DMA" as DMA
participant "NVIC" as nvc
participant "HW" as hw

user->JPEG : <b>HAL_JPEG_Init()</b>
user <-- JPEG : HAL_OK
JPEG->DMA : <b>__HAL_LINKDMA()</b>
user->JPEG : <b>HAL_JPEG_ConfigEncoding()</b>
user <-- JPEG : HAL_OK
user->JPEG : <b>HAL_JPEG_Encode_DMA()</b>
user <-- JPEG : HAL_OK

hw -> nvc : <<DMAIn Transfer complete interrupt>>
nvc -> DMA : HAL_DMA_IRQHandler()
DMA -> JPEG : JPEG_DMAInCpltCallback()

user <- JPEG : <b>HAL_JPEG_GetDataCallback()</b>\nIs asserted when the input buffer has been consumed by the peripheral and to ask for a new data chunk
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, when HAL_JPEG_GetDataCallback is received, the user can pause the process and change the input buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigInputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT)</b>\nAt this stage, the user can resume the process after changing the input buffer.
  user <-- JPEG :
end

hw -> nvc : <<DMAOut Transfer complete interrupt>>
nvc -> DMA : HAL_DMA_IRQHandler()
DMA -> JPEG : JPEG_DMAOutCpltCallback()

user <- JPEG : <b>HAL_JPEG_DataReadyCallback()</b>\nIs asserted when the HAL JPEG driver has filled the given output buffer with the given size
group #LightYellow Optional
  user -> JPEG : <b>HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, when HAL_JPEG_DataReadyCallback is received, the user can pause the process and change the output buffer.
  user <-- JPEG :
end

group #LightYellow Optional
  user -> JPEG : HAL_JPEG_ConfigOutputBuffer()
  user <-- JPEG :
  user -> JPEG : <b>HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT)</b>\nAt this stage, the user can resume the process after changing the output buffer.
  user <-- JPEG :
end

hw -> nvc : <<End of conversion interrupt>>
nvc -> JPEG : HAL_JPEG_IRQHandler()

user <- JPEG : <b>HAL_JPEG_EncodeCpltCallback()</b>\nIs asserted when the HAL JPEG driver has ended the current JPEG encoding operation

@enduml

code snippet

Example of code in HAL 1

Example of code in HAL 2

/* Declaration of the JPEG handle */
JPEG_HandleTypeDef hjpeg;
/* Declaration of the JPEG config */
JPEG_ConfTypeDef jpeg_config;

/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // YCbCr image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Encoded JPEG output buffer

/* Ensure that all required peripherals (JPEG, DMA) are configured properly before starting encoding */
/* This includes clock enabling, interrupt priority setup. */

/* Initialize the JPEG peripheral */
HAL_JPEG_Init(&hjpeg);

/* Configure encoding parameters */
jpeg_config.ColorSpace = JPEG_YCBCR_COLORSPACE;         /* YCbCr color space */
jpeg_config.ChromaSubsampling = JPEG_420_SUBSAMPLING;   /* 4:2:0 chroma subsampling */
jpeg_config.ImageHeight = IMAGE_HEIGHT;                  /* Image height in pixels */
jpeg_config.ImageWidth  = IMAGE_WIDTH;                   /* Image width in pixels */
jpeg_config.ImageQuality = 75;                           /* JPEG quality from 1 to 100 */

HAL_JPEG_ConfigEncoding(&hjpeg, &jpeg_config);

/* Start JPEG encoding in DMA mode */
HAL_JPEG_Encode_DMA(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer));

/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_ConfigInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_INPUT);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut,
                                uint32_t OutDataLength)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_ConfigOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_Resume(hjpeg, JPEG_PAUSE_RESUME_OUTPUT);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
/* Declaration of the JPEG handle */
hal_jpeg_handle_t hjpeg;
/* Declaration of the JPEG config */
hal_jpeg_config_t jpeg_config;

/* Input and output buffers */
uint8_t input_buffer[INPUT_BUFFER_SIZE];   // YCbCr image data in MCU blocks
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; // Encoded JPEG output buffer

/* Ensure that all required peripherals (JPEG, DMA) are configured properly before starting encoding */
/* This includes clock enabling, interrupt priority setup. */

/* Initialize the JPEG peripheral and associate the handle */
HAL_JPEG_Init(&hjpeg, HAL_JPEG);

/* Configure encoding parameters */
jpeg_config.ColorSpace = JPEG_YCBCR_COLORSPACE;         /* YCbCr color space */
jpeg_config.input_format = HAL_JPEG_IN_FORMAT_YCBCR_420; // Example format
jpeg_config.image_height = IMAGE_HEIGHT;                 // Image height in pixels
jpeg_config.image_width  = IMAGE_WIDTH;                  // Image width in pixels
jpeg_config.image_quality = 75;                           // JPEG quality from 1 to 100

HAL_JPEG_SetConfigEncoding(&hjpeg, &jpeg_config);

/* Start JPEG encoding in DMA mode */
HAL_JPEG_Encode_DMA(&hjpeg, input_buffer, sizeof(input_buffer),
                   output_buffer, sizeof(output_buffer));

/* Callback invoked when the input buffer has been consumed by the peripheral */
void HAL_JPEG_GetDataCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Optional block: pause input buffer if waiting for new data */
    HAL_JPEG_PauseInputBuffer(hjpeg);
    /* Optional block: when new input data is ready, update and resume input buffer */
    HAL_JPEG_UpdateInputBuffer(hjpeg, new_input_ptr, new_input_size);
    HAL_JPEG_ResumeInputBuffer(hjpeg);
}

/* Callback invoked when the output buffer has been filled by the peripheral */
void HAL_JPEG_DataReadyCallback(hal_jpeg_handle_t *hjpeg, const uint8_t *p_data_out,
                                uint32_t out_data_length_byte)
{
    /* Process or store the output data chunk */
    /* Optional block: pause output buffer if processing not ready */
    HAL_JPEG_PauseOutputBuffer(hjpeg);
    /* Optional block: update and resume output buffer when ready */
    HAL_JPEG_UpdateOutputBuffer(hjpeg, new_output_ptr, new_output_size);
    HAL_JPEG_ResumeOutputBuffer(hjpeg);
}

/* Callback invoked when encoding is complete */
void HAL_JPEG_EncodeCpltCallback(hal_jpeg_handle_t *hjpeg)
{
    /* Encoding complete, output_buffer contains the full JPEG image */
    /* User can now process or save the JPEG image */
    /* For example, toggle an LED */
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}

Related examples in HAL2

Example Name

Description

hal_jpeg_encoding_dma

How to encode an RGB image from internal FLASH using the JPEG hardware encoder in DMA mode and save it to internal FLASH.


Note

  • Other encoding use cases (polling and interrupt modes) follow a pattern very similar to the DMA encoding use case; only the function calls need to be adapted accordingly.

  • For example, instead of using HAL_JPEG_Encode_DMA(), HAL_JPEG_Encode_IT() is used for interrupt mode and HAL_JPEG_Encode() for polling mode.